﻿using System;
using System.Collections.Generic;
using System.Text;
using Graffiti.Core;
using DataBuddy;
using System.Data;

namespace GraffitiExtras.Extensions.PostNavigator
{
    [Chalk("postNavigator")]
    public class PostNavigator
    {
        private const string linkFormatPrevious =
            "<a href=\"{0}\" title=\"{3}\">{2}{1}</a>";
        private const string linkFormatNext =
            "<a href=\"{0}\" title=\"{3}\">{1}{2}</a>";

        Post _previous = null;
        Post _next = null;

        public string GetNavigator(Post post, string previousText, string nextText)
        {
            return GetNavigator(post, true, true, " | ", previousText, nextText, "« ", " »");
        }

        public string GetNavigator(Post post, bool showPrevious, bool showNext,
            string separator, string previousText, string nextText,
            string previousAppendText, string nextAppendText)
        {
            if ((!showPrevious) && (!showNext))
            {
                Log.Error("Post Navigator Extension",
                    "What do you want to get? One of the next or previous posts must be shown!");
                return string.Empty;
            }

            try
            {
                if (showPrevious)
                    this._previous = GetPreviousPost(post);
                else
                    this._previous = null;
                if (showNext)
                    this._next = GetNextPost(post);
                else
                    this._next = null;
            }
            catch (Exception ex)
            {
                Log.Error("Post Navigator Extension", ex.Message + ex.StackTrace);
                return string.Empty;
            }

            return GetHtmlCode(separator, previousText, nextText,
                previousAppendText, nextAppendText);
        }

        private Post GetPreviousPost(Post post)
        {
            Query query = new Query(Post.Table);

            query.AndWhere(Post.Columns.Id, post.Id, Comparison.LessThan);
            query.AndWhere(Post.Columns.IsPublished, false, Comparison.NotEquals);
            query.OrderByDesc(Post.Columns.Id);

            PostCollection posts = PostCollection.FetchByQuery(query);

            if (posts.Count > 0)
                return PostCollection.FetchByQuery(query)[0];
            else
                return null;
        }

        private Post GetNextPost(Post post)
        {
            Query query = new Query(Post.Table);

            query.AndWhere(Post.Columns.Id, post.Id, Comparison.GreaterThan);
            query.AndWhere(Post.Columns.IsPublished, false, Comparison.NotEquals);
            query.OrderByAsc(Post.Columns.Id);

            PostCollection posts = PostCollection.FetchByQuery(query);

            if (posts.Count > 0)
                return PostCollection.FetchByQuery(query)[0];
            else
                return null;
        }

        private string GetHtmlCode(string separator, string previousText,
            string nextText, string previousAppendText, string nextAppendText)
        {
            string result = string.Empty;

            try
            {
                if (this._previous != null)
                {
                    string linkText = this._previous.Title;

                    if (!string.IsNullOrEmpty(previousText))
                        linkText = previousText;

                    result += string.Format(linkFormatPrevious,
                        new Macros().FullUrl(this._previous.Url),
                        linkText, previousAppendText, this._previous.Title);
                }

                if ((this._previous != null) && (this._next != null))
                    result += separator;

                if (this._next != null)
                {
                    string linkText = this._next.Title;

                    if (!string.IsNullOrEmpty(nextText))
                        linkText = nextText;

                    result += string.Format(linkFormatNext,
                        new Macros().FullUrl(this._next.Url),
                        linkText, nextAppendText, this._next.Title);
                }
            }
            catch (Exception ex)
            {
                Log.Error("Post Navigator Extension", ex.Message + ex.StackTrace);
                return string.Empty;
            }

            return result;
        }
    }
}
